package com.doublethread.easymart.config;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.List;

/**
 * OSS配置类
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "oss")
public class OssConfig {

    /**
     * OSS服务端点
     */
    private String endpoint;

    /**
     * 访问密钥ID
     */
    private String accessKeyId;

    /**
     * 访问密钥Secret
     */
    private String accessKeySecret;

    /**
     * 存储桶名称
     */
    private String bucketName;

    /**
     * 图片上传路径前缀
     */
    private String imagePath = "images/";

    /**
     * 允许的图片格式
     */
    private String allowedFormats = "jpg,jpeg,png,gif,webp";

    /**
     * 最大文件大小（MB）
     */
    private Integer maxFileSize = 5;

    /**
     * 创建OSS客户端
     */
    @Bean
    public OSS ossClient() {
        return new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    }

    /**
     * 获取允许的图片格式列表
     */
    public List<String> getAllowedFormatList() {
        return Arrays.asList(allowedFormats.toLowerCase().split(","));
    }

    /**
     * 获取最大文件大小
     */
    public long getMaxFileSizeBytes() {
        return maxFileSize * 1024L * 1024L;
    }
}
